#include <stdlib.h>
#include <stdio.h>
#include <string.h>

static void __float_to_char(char *str, double number, unsigned char g, unsigned char l)
{
    unsigned char i;
    int temp = number/1;
    double t2 = 0.0;
	char table[]={'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};

    for (i = 1; i<=g; i++)
    {
        if (temp==0)
            str[g-i] = table[0];
        else
            str[g-i] = table[temp%10];
        temp = temp/10;
    }

    *(str+g) = '.';
    temp = 0;
    t2 = number;

    for(i=1; i<=l; i++)
    {
        temp = t2*10;
        str[g+i] = table[temp%10];
        t2 = t2*10;
    }

    *(str+g+l+1) = '\0';
}

char * float_to_char(double num, char * str)
{
    __float_to_char(str, num, 5, 6);
    //printf("%s\n", str);

    return str;
}


void Float2Char(float value, unsigned char *array)
{
      unsigned short IntegerPart;  //整数部分
      float DecimalPart;//小数部分
      unsigned char i = 0;
      unsigned char j = 0;
      unsigned short temp;
      unsigned char bit = 0;
      //判断正负数
      if(value < 0)
      {
              value =0.0-value;
              array[0] = '-';
              bit = 1; //标记负数
              i++;
      }
      //分离整数和小数
      if(value >= 1)//大于1的浮点数
      {
              IntegerPart = (unsigned short)value;  //传入的值强制转换为整数
      }
      else
      {
              IntegerPart = 0;//小于1的浮点数 
      }
      DecimalPart = value - IntegerPart;//获取小数部分
      //处理整数部分
      if(IntegerPart == 0) //小于1的浮点数
      {
              if(bit)//如果是负数
              {
                      array[1] = '0';
                      array[2] = '.';
                      i = 2;
              }
              else
              {
                      array[0] = '0';
                      array[1] = '.';
                      i = 1;
              }
      }
      else //大于1的浮点数
      {
              while(IntegerPart > 0)
              {
                      array[i++] = IntegerPart % 10 + '0'; //
                      IntegerPart /= 10;
              }
              i--;//退出来记录前一个位置


              //修正数据位置
              if(bit)//为负数的时候
              {
                      if(i%2==0)  //位数为偶数
                      {
                              for(j=1; j<=i/2; j++) //最中间的数字位置不变
                              {
                                      temp = array[j];
                                      array[j] = array[i+1 - j];
                                      array[i+1 - j] = temp;
                              }
                      }
                      else//位数为奇数
                              for(j=1; j<=(i-1)/2; j++)
                              {
                                      temp = array[j];
                                      array[j] = array[i+1 - j];
                                      array[i+1 - j] = temp;
                              }
              }
              else	//为整数
                      if(i%2==0)  //位数为奇数
                      {
                              for(j=0; j<i/2; j++) //最中间的数字位置不变
                              {
                                      temp = array[j];
                                      array[j] = array[i - j];
                                      array[i - j] = temp;
                              }
                      }
                      else//位数为偶数
                              for(j=0; j<(i+1)/2; j++)
                              {
                                      temp = array[j];
                                      array[j] = array[i - j];
                                      array[i - j] = temp;
                              }
              array[++i] = '.';//添加小数点
      }
      //小数部分处理
      i++;
      array[i++] = (unsigned char)(DecimalPart * 10)%10 + '0';
      array[i++] = (unsigned char)(DecimalPart * 100)%10 + '0';
	  array[i++] = (unsigned char)(DecimalPart * 1000)%10 + '0';
      array[i]   = '\0';
} 

int main()
{
	char str[20] = {0} ;
	char *p = NULL;
	double df = -28.345;
	char fu[20] = {0};

	p = float_to_char(28.345, str);
	printf("str:%s, p:%s\n", str, p);	// str:00028.345000, p:00028.345000

	p = float_to_char(-28.345, str);
	printf("str:%s, p:%s\n", str, p);	// str:000, p:000

	if (df < 0.000) {
		printf("%f\n", df);				// -28.345000
		df = -(df);
		printf("%f\n", df);				// 28.345000

		p = float_to_char(df, str);
		printf("str:%s, p:%s\n", str, p);	// str:00028.345000, p:00028.345000
		
		sprintf(fu, "-%s", str);
		printf("fu:%s\n", fu);				// fu:-00028.345000

	}

	df = -0011.123;
	printf("%f\n", df);		// -11.123000
	printf("%f\n", -df);	// 11.123000

	memset(str, 0, 20);
	Float2Char(df, str);
	printf("%s\n", (str));	// -11.12

	memset(str, 0, 20);
	Float2Char(-df, str);
	printf("%s\n", (str));	// 11.12

	df = -000.0000000000000000000000000;
	memset(str, 0, 20);
	Float2Char(df, str);
	printf("%s\n", (str));
	
	df = 0.9939;
	memset(str, 0, 20);
	Float2Char(-df, str);
	printf("%s\n", (str));
	
	return 0;
}
